local LENS_NAME = "AUSExplorer"
local LENS_LAYER = UILens.CreateLensLayerHash("Hex_Coloring_Appeal_Level")
local AUS_EXPLORER_INDEX = GameInfo.Units['UNIT_AUS_EXPLORER'].Index

local function plotHasGoodyHut(plot)
  local improvementInfo = GameInfo.Improvements[plot:GetImprovementType()]
  if improvementInfo ~= nil and improvementInfo.ImprovementType == "IMPROVEMENT_GOODY_HUT" then
    return true
  end
  return false
end

local function OnGetColorPlotTable()
  local mapWidth, mapHeight = Map.GetGridSize()
  local localPlayer:number = Game.GetLocalPlayer()
  local localPlayerVis:table = PlayersVisibility[localPlayer]

  local GoodyHutColor:number = UI.GetColorValue("COLOR_GHUT_SCOUT_LENS")
  local colorPlot = {}
  colorPlot[GoodyHutColor] = {}

  for i = 0, (mapWidth * mapHeight) - 1, 1 do
    local pPlot:table = Map.GetPlotByIndex(i)
    if localPlayerVis:IsRevealed(pPlot:GetX(), pPlot:GetY()) then
      if plotHasGoodyHut(pPlot) then
        table.insert(colorPlot[GoodyHutColor], i)
      end
    end
  end

  return colorPlot
end

local function ShowAUSExplorerLens()
  LuaEvents.MinimapPanel_SetActiveModLens(LENS_NAME)
  UILens.ToggleLayerOn(LENS_LAYER)
end

local function ClearAUSExplorerLens()
  if UILens.IsLayerOn(LENS_LAYER) then
    UILens.ToggleLayerOff(LENS_LAYER)
  end
  LuaEvents.MinimapPanel_SetActiveModLens("NONE")
end

local function RefreshAUSExplorerLens()
  ClearAUSExplorerLens()
  ShowAUSExplorerLens()
end

local function OnUnitSelectionChanged( playerID:number, unitID:number, hexI:number, hexJ:number, hexK:number, bSelected:boolean, bEditable:boolean )
  if playerID ~= Game.GetLocalPlayer() then
    return
  end

  local pPlayer = Players[playerID]
  if pPlayer == nil then
    return
  end

  local pUnit = pPlayer:GetUnits():FindID(unitID)
  if pUnit == nil then
    return
  end

  local unitType = pUnit:GetUnitType()
  if unitType ~= AUS_EXPLORER_INDEX then
    return
  end

  if bSelected then
    ShowAUSExplorerLens()
  else
    ClearAUSExplorerLens()
  end
end

local function OnUnitRemovedFromMap( playerID: number, unitID : number )
  if playerID ~= Game.GetLocalPlayer() then
    return
  end

  local lens = {}
  LuaEvents.MinimapPanel_GetActiveModLens(lens)
  if lens[1] == LENS_NAME then
    ClearAUSExplorerLens()
  end
end

local function OnUnitMoveComplete( playerID:number, unitID:number )
  if playerID ~= Game.GetLocalPlayer() then
    return
  end

  local pPlayer = Players[playerID]
  if pPlayer == nil then
    return
  end

  local pUnit = pPlayer:GetUnits():FindID(unitID)
  if pUnit == nil then
    return
  end

  if not UI.IsUnitSelected(pUnit) then
    return
  end

  local unitType = pUnit:GetUnitType()
  if unitType ~= AUS_EXPLORER_INDEX then
    return
  end
  
  RefreshAUSExplorerLens()
end

local function OnGoodyHutReward( playerID:number )
  if playerID ~= Game.GetLocalPlayer() then
    return
  end

  local lens = {}
  LuaEvents.MinimapPanel_GetActiveModLens(lens)
  if lens[1] == LENS_NAME then
    RefreshAUSExplorerLens()
  end
end

local function OnInitialize()
  Events.UnitSelectionChanged.Add(OnUnitSelectionChanged)
  Events.UnitRemovedFromMap.Add(OnUnitRemovedFromMap)
  Events.UnitMoveComplete.Add(OnUnitMoveComplete)
  Events.GoodyHutReward.Add(OnGoodyHutReward)
end

local AUSExplorerLensEntry = {
  LensButtonText = "LOC_HUD_AUS_EXPLORER_LENS",
  LensButtonTooltip = "LOC_HUD_AUS_EXPLORER_LENS_TOOLTIP",
  Initialize = OnInitialize,
  GetColorPlotTable = OnGetColorPlotTable
}

-- minimappanel.lua
if g_ModLenses ~= nil then
  g_ModLenses[LENS_NAME] = AUSExplorerLensEntry
end

-- modallenspanel.lua
if g_ModLensModalPanel ~= nil then
  g_ModLensModalPanel[LENS_NAME] = {}
  g_ModLensModalPanel[LENS_NAME].LensTextKey = "LOC_HUD_AUS_EXPLORER_LENS"
  g_ModLensModalPanel[LENS_NAME].Legend = {
      {"LOC_TOOLTIP_SCOUT_LENS_GHUT", UI.GetColorValue("COLOR_GHUT_SCOUT_LENS")}
  }
end